package com.cms.intelligent_control.views;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

/**
 * Created by 12457 on 2017/8/29.
 */

public class SuperFileView extends FrameLayout implements TbsReaderView.ReaderCallback {

    private final static String TAG = "SuperFileView";
    private TbsReaderView mTbsReaderView;
    private Context mContext;

    public SuperFileView(Context context) {
        this(context, null, 0);
    }

    public SuperFileView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SuperFileView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTbsReaderView = new TbsReaderView(context, this);
        this.addView(mTbsReaderView, new LinearLayout.LayoutParams(-1, -1));
        this.mContext = context;
    }

    private TbsReaderView getTbsReaderView(Context context) {
        return new TbsReaderView(context, this);
    }

    public void displayFile(File file) {
        if (file != null) {

            //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
            String bsReaderTemp = Environment.getExternalStorageDirectory() + "/" + "TbsReaderTemp";
            File bsReaderTempFile = new File(bsReaderTemp);
            if (!bsReaderTempFile.exists()) {
                Log.d(TAG, "准备创建" + bsReaderTemp + "！！");
                boolean mkdir = bsReaderTempFile.mkdir();
                if (!mkdir) {
                    Log.e(TAG, "创建" + bsReaderTemp + "失败！！！！！");
                }
            }

            //加载文件
            Bundle localBundle = new Bundle();
            Log.d(TAG, file.toString());
            localBundle.putString("filePath", file.getPath());
            localBundle.putString("tempPath", Environment.getExternalStorageDirectory() + "/" + "TbsReaderTemp");
            if (this.mTbsReaderView == null) {
                this.mTbsReaderView = getTbsReaderView(mContext);
            }
            boolean bool = this.mTbsReaderView.preOpen(getFileType(file.getPath()), false);
            if (bool) {
                this.mTbsReaderView.openFile(localBundle);
            }
        } else {
            Log.e(TAG, "文件路径无效！");
        }

    }

    /***
     * 获取文件类型
     *
     * @param filename
     * @return
     */
    private String getFileType(String filename) {
        String str = "";
        if (TextUtils.isEmpty(filename)) {
            return str;
        }
        int i = filename.lastIndexOf('.');
        if (i <= -1) {
            return str;
        }
        str = filename.substring(i + 1);
        return str;
    }


    public void onStopDisplay() {
        if (mTbsReaderView != null) {
            mTbsReaderView.onStop();
        }
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {

    }
}
